package com.zyz.O_网络编程.UDP.demo1;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
 * @author: vick
 * @Date: 2025/4/17
 * @Description:
 * @version: 1.0
 */
public class Receive {
    public static void main(String[] args) {
        DatagramSocket datagramSocket = null;
        try {
            datagramSocket = new DatagramSocket(8888);
            byte[] bytes = new byte[1024 * 64];
            DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length);
            datagramSocket.receive(datagramPacket);
            String str = new String(datagramPacket.getData(), 0, datagramPacket.getLength());
            System.out.println(str + ":" + datagramPacket.getAddress());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            if (datagramSocket != null) {
                datagramSocket.close();
            }
        }
    }
}
